package org.jeecg.modules.yw.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.yw.entity.Messages;
import org.jeecg.modules.yw.entity.Rewardspunish;
import org.jeecg.modules.yw.service.IRewardspunishService;
import org.jeecg.modules.yw.service.MessagesService;
import org.jeecg.modules.yw.utils.CommonUtils;
import org.jeecg.modules.yw.utils.DateUtils;
import org.jeecg.modules.yw.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description: 奖惩通知表
 * @Author: jeecg-boot
 * @Date: 2025-04-10
 * @Version: V1.0
 */
@Api(tags = "奖惩通知表")
@RestController
@RequestMapping("/yw/rewardspunish")
@Slf4j
public class RewardspunishController extends JeecgController<Rewardspunish, IRewardspunishService> {
    @Autowired
    private IRewardspunishService rewardspunishService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    
    @Autowired
    private MessagesService messageService;

    /**
     * 分页列表查询
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @param
     * @return
     */
    //@AutoLog(value = "奖惩通知表-分页列表查询")
    @ApiOperation(value = "奖惩通知表-分页列表查询", notes = "奖惩通知表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<Rewardspunish>> queryPageList(@RequestParam(name = "content",required = false) String content,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<Rewardspunish> queryWrapper = new QueryWrapper<>();
        if (oConvertUtils.isNotEmpty(content)) {
            queryWrapper.like("content", content);
        }
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<String> roleIds = sysBaseAPI.getRoleIdsByUsername(sysUser.getUsername());
        if (!roleIds.contains(CommonUtils.ADMIN)) {
            String area=sysUser.getArea();
            if(org.apache.commons.lang3.StringUtils.isNotBlank(area)){
                String[] areas=area.split(",");
                StringBuffer st=new StringBuffer();
                for (int i = 0; i < areas.length; i++) {
                    st.append("\""+areas[i]+"\",");
                }
                st=st.deleteCharAt(st.length()-1);
                String sql="(select id from enterprise where city_code in ("+st.toString()+") " +
                        "or (province_code in  ("+st.toString()+") and city_code is null))";
                queryWrapper.apply("enterid in"+sql+"");
            }else{
                IPage<Rewardspunish> result =new Page<>();
                return Result.OK(result);
            }
        }
        queryWrapper.orderByDesc("createtime");
        Page<Rewardspunish> page = new Page<>(pageNo, pageSize);
        IPage<Rewardspunish> pageList = rewardspunishService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param rewardspunish
     * @return
     */
    @AutoLog(value = "奖惩通知表-添加")
    @ApiOperation(value = "奖惩通知表-添加", notes = "奖惩通知表-添加")
    //@RequiresPermissions("org.jeecg.modules:rewardspunish:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody Rewardspunish rewardspunish, HttpServletRequest request) {
        rewardspunish.setId(CommonUtils.getID());
        rewardspunish.setCreator(UserUtil.getUserId(request, sysBaseAPI));
        rewardspunish.setCreatetime(DateUtils.getCurrentDate());
        rewardspunishService.save(rewardspunish);
        String str = rewardspunish.getEnterid();
        String arr[] = str.split(",");
        
        for (String id : arr) {
			Messages message = new Messages();
			message.setId(CommonUtils.getID());
			message.setMsgType(2);
			message.setNormalMsgType(8);
			message.setState(0);
			message.setCurrentUserId(id);
			message.setMsgTime(DateUtils.getCurrentTimeDateStr());
			message.setDelFlag(0);
			message.setRemark(rewardspunish.getContent());
			messageService.save(message);
        }        
        
        return Result.OK("添加成功！");
    }
    /**
     * 添加
     *
     * @param rewardspunish
     * @return
     */
    @AutoLog(value = "奖惩通知表-新添加")
    @ApiOperation(value = "奖惩通知表-添加", notes = "奖惩通知表-添加")
    //@RequiresPermissions("org.jeecg.modules:rewardspunish:add")
    @PostMapping(value = "/addn")
    public Result<String> addn(@RequestBody Rewardspunish rewardspunish, HttpServletRequest request) {
//        List<String> enterids = rewardspunish.getEnterids();
//        List<Rewardspunish> list= new ArrayList<>();
//        for (String enterid : enterids) {
//            Rewardspunish rewardspunish1 = new Rewardspunish();
//            rewardspunish1.setId(CommonUtils.getID());
//            rewardspunish1.setCreator(UserUtil.getUserId(request, sysBaseAPI));
//            rewardspunish1.setCreatetime(DateUtils.getCurrentDate());
//            rewardspunish1.setEnterid(enterid);
//            rewardspunish1.setContent(rewardspunish.getContent());
//            rewardspunish1.setRptime(rewardspunish.getRptime());
//            list.add(rewardspunish1);
//        }
//        rewardspunishService.saveBatch(list);
        return Result.OK("添加成功！");
    }
    /**
     * 编辑
     *
     * @param rewardspunish
     * @return
     */
    @AutoLog(value = "奖惩通知表-编辑")
    @ApiOperation(value = "奖惩通知表-编辑", notes = "奖惩通知表-编辑")
    //@RequiresPermissions("org.jeecg.modules:rewardspunish:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody Rewardspunish rewardspunish) {
        rewardspunishService.updateById(rewardspunish);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "奖惩通知表-通过id删除")
    @ApiOperation(value = "奖惩通知表-通过id删除", notes = "奖惩通知表-通过id删除")
    //@RequiresPermissions("org.jeecg.modules:rewardspunish:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        rewardspunishService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "奖惩通知表-批量删除")
    @ApiOperation(value = "奖惩通知表-批量删除", notes = "奖惩通知表-批量删除")
    //@RequiresPermissions("org.jeecg.modules:rewardspunish:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.rewardspunishService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "奖惩通知表-通过id查询")
    @ApiOperation(value = "奖惩通知表-通过id查询", notes = "奖惩通知表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<Rewardspunish> queryById(@RequestParam(name = "id", required = true) String id) {
        Rewardspunish rewardspunish = rewardspunishService.getById(id);
        if (rewardspunish == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(rewardspunish);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param rewardspunish
     */
    //@RequiresPermissions("org.jeecg.modules:rewardspunish:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Rewardspunish rewardspunish) {
        return super.exportXls(request, rewardspunish, Rewardspunish.class, "奖惩通知表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("rewardspunish:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Rewardspunish.class);
    }

}
